package com.max.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.pagehelper.PageHelper;
import com.max.base.BaseController;
import com.max.common.R;
import com.max.dto.UserInfoAddDTO;
import com.max.dto.UserInfoUpdateDTO;
import com.max.entity.UserInfo;
import com.max.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Function;


@Api(tags = "用户管理相关接口", value = "用户管理相关接口")
@RequestMapping(value = "/user")
@RestController
public class UserInfoController extends BaseController {

    private final IUserInfoService userInfoService;

    public UserInfoController(IUserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户名（支持模糊查询）", name = "username", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "登录名称", name = "loginName", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "角色类型", name = "roleCode", dataType = "String", paramType = "query"),
    })
    @ApiOperation("查询用户列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<UserInfo>> selectUserList(String username, String loginName, String roleCode) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<UserInfo>()
                .eq(StringUtils.isNotBlank(username), UserInfo::getUsername, username)
                .eq(StringUtils.isNotBlank(loginName), UserInfo::getLoginName, loginName)
                .eq(StringUtils.isNotBlank(roleCode), UserInfo::getRoleCode, roleCode);
        startPage();
        List<UserInfo> list = userInfoService.list(lqw);
        return R.ok(list);
    }


    @ApiOperation("添加用户")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R<?> addUser(@RequestBody UserInfoAddDTO userInfoDTO) {
        UserInfo insertInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoDTO, insertInfo);
        insertInfo.setRoleCode(userInfoDTO.getRoleType().name());
        insertInfo.setRoleName(userInfoDTO.getRoleType().getRoleName());
        boolean save = userInfoService.save(insertInfo);
        return R.result(save);
    }

    @ApiOperation("根据用户ID更新用户")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public R<?> updateUserById(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
        UserInfo updateInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoUpdateDTO, updateInfo);
        updateInfo.setRoleCode(userInfoUpdateDTO.getRoleType().name());
        updateInfo.setRoleName(userInfoUpdateDTO.getRoleType().getRoleName());
        return R.result(userInfoService.updateById(updateInfo));
    }

    @ApiOperation("根据用户ID删除用户")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public R<?> deleteById(@PathVariable("id") Long id) {
        return R.result(userInfoService.removeById(id));
    }

}
